import lodash from 'lodash';

export default ({ required, Rules }: any) => {
  const newRule = [
    {
      required,
      message: 'required',
    },
    ...Rules,
  ];

  return newRule.map((rule) => {
    return lodash.isFunction(rule)
      ? {
          validator: (_rule: any, value: any) => {
            return new Promise((resolve, reject) => {
              rule(_rule, value, (msg: string | undefined) => {
                if (msg) {
                  //校验失败
                  reject(new Error(msg));
                }
                //校验成功
                resolve(Promise.resolve());
              });
            });
          },
        }
      : rule;
  });
};
